﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;


//实现三个拖拽的接口 两个悬停接口
public class Item : MonoBehaviour,IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
{
	private Vector3 beforpos;
	private Vector3 startpos;
	public GameObject item_box = null;

	public Item_data data;
	//public GameObject text;

	private RectTransform parentRectTransform;


	/// <summary>
    /// 用于event trigger对自身检测的开关
    /// </summary>
    private CanvasGroup canvasGroup;

	// Use this for initialization
	void Start ()
	{
		parentRectTransform = transform.parent as RectTransform;
		canvasGroup = GetComponent<CanvasGroup>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void Ini(GameObject box)
	{
		Item_box box_sc = (Item_box)box.GetComponent<Item_box>();
        data = new Item_data(box_sc.item_ID);
        transform.position = box.transform.position;
        int num = Random.Range(1,4); // 获得对应的图标
        Sprite sp = Resources.Load<Sprite>("icon/item"+num.ToString("G"));
        GetComponent<Image>().overrideSprite = sp;
        GetComponent<Item>().item_box = box;
	}

	public void Refresh_num(int num)
	{
		GetComponentInChildren<Text>().text = num.ToString("G");
	}


	public void OnBeginDrag(PointerEventData eventData)
	{

		canvasGroup.blocksRaycasts = false;
		transform.SetAsLastSibling();
		beforpos = (transform as RectTransform).position;
		startpos = (transform as RectTransform).position;
	}

	public void OnDrag(PointerEventData eventData)
	{
		Vector2 pos;
		RectTransformUtility.ScreenPointToLocalPointInRectangle(
			parentRectTransform,
			eventData.position, 
			eventData.pressEventCamera,
			out pos);
		(transform as RectTransform).anchoredPosition = pos;
		GameObject curEnter = eventData.pointerEnter;
		if( (curEnter != null) && (curEnter.tag == "item_box") )
		{
			beforpos = curEnter.transform.position;
		}
	}

	public void OnEndDrag(PointerEventData eventData)
	{
		GameObject curEnter = eventData.pointerEnter;
		if(curEnter == null)
		{
			transform.position = beforpos;
		}
		else
		{	
			Item_box item_box_sc = item_box.GetComponent<Item_box>();
			Item_box curEnter_sc;
			GameObject gobj;
			switch (curEnter.tag) 
			{
				case "item_box":
				  curEnter_sc = curEnter.GetComponent<Item_box>();

				  transform.position = curEnter.transform.position;
				  item_box_sc.swap_date(curEnter_sc);
				  item_box = curEnter;
				  break;
				case "item":
					Debug.Log(curEnter.name);
				  //curEnter = curEnter.transform.parent.gameObject;
				  curEnter_sc = curEnter.GetComponent<Item>().item_box.GetComponent<Item_box>();

				  transform.position = curEnter.transform.position;
				  curEnter.transform.position = startpos;

				  item_box_sc.swap_date(curEnter_sc);
				  gobj = item_box;
				  item_box = curEnter.GetComponent<Item>().item_box;
				  curEnter.GetComponent<Item>().item_box = gobj;
				  break;
				default:
				  transform.position = beforpos;
				  break;	
			}
		}
		canvasGroup.blocksRaycasts = true;	
	}




	public void OnPointerExit(PointerEventData eventData)
	{
        Gamecontrol.Instance.itemtips.SetActive(false);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
    	if (Input.GetMouseButton(1)) 
    	{
    		//useitem
    	}
        //鼠标悬在同一物品上，则已经显示的tips保持原样，避免闪烁
        if (eventData.lastPress == eventData.pointerEnter)
        {
            return;
        }
        GameObject tips = Gamecontrol.Instance.itemtips;
        if (!tips.activeSelf)
        {
        	tips.transform.position = transform.position;
        	tips.GetComponentInChildren<Text>().text = get_tips_text(item_box.GetComponent<Item_box>());
        	tips.SetActive(true);
        }
    }

    public string get_tips_text(Item_box box)
    {
        string tips = "";
        tips += "物品id：<color=green>" + data.ID + "</color>\n";
        tips += "  数量：<color=green>" + box.num + "/" + box.max_num + "</color>\n";
        tips += data.note;
        return tips;
    }
}
